﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.Common;
using System.Text.RegularExpressions;
using TVGuide.DataAccess.Common;
using TVGuide.DataAccess.Repository;

namespace TVGuide.Parser
{
    public class EterParserEngine
    {

        public static List<Program> ParseSpecificProgram(Channel channel, DateTime date)
        {
            //shoxakat/today.shtml
            List<Program> tvProgramList = new List<Program>();
            string eterURL;
            //Western European (Windows) encoding
            Encoding enc = Encoding.GetEncoding(1252);
            if ((int)date.DayOfWeek==0)
            {
                eterURL = ChannelUrls.ETER_ARM_URL + "/" + channel.Name + "/" + 7 + ".shtml";
            }
            else
            {
                eterURL = ChannelUrls.ETER_ARM_URL + "/" + channel.Name + "/" + (int)date.DayOfWeek + ".shtml";
            }
            
            var eterHTMLString = HTTPClient.GetSiteContent(eterURL, enc, "EterAmSite");
            var unicodeString = TVGuide.Common.Utils.ArmToUnicode(eterHTMLString);

            string programTitleStringPattern = "<font face=\"Arial AM, Arial Armenian, Dallak Time\"  size=\"2\" color=\"#000000\">.*?</font>";
            Regex rgxProgramDatesTitleStringsWithHTML = new Regex(programTitleStringPattern, RegexOptions.IgnoreCase);
            MatchCollection programTitlesWithHTML = rgxProgramDatesTitleStringsWithHTML.Matches(unicodeString);
            string name = "";
            string time = "";
            int hour;
            int minute;
            int count = 0;
            while (count < programTitlesWithHTML.Count)
            {
                time = Utils.removeHTMLTags(programTitlesWithHTML[count].Value);
                name = Utils.removeHTMLTags(programTitlesWithHTML[count+1].Value);

                string programDateExtractor = @"\d+\.\d{2}"; 
                Regex datesForSameProgram = new Regex(programDateExtractor, RegexOptions.IgnoreCase);
                MatchCollection dates = datesForSameProgram.Matches(name);
                string filteredName = "";
                if (dates.Count != 0)
                {
                    tvProgramList.AddRange(progNameToProgObjects(name, channel, date, out filteredName));
                    // crete program with date=>time and title=>filteredName
                    Utils.seperateTimeProgramName(time, out hour, out minute);
                    Program tvProgram = new Program();
                    tvProgram.Name = filteredName.Trim();
                    tvProgram.Date = new DateTime(
                        date.Year,
                        date.Month,
                        date.Day,
                        hour,
                        minute,
                        0
                        );
                    tvProgram.ChannelId = channel.Id;
                    tvProgramList.Add(tvProgram);
                    var a = name;
                }
                else
                {
                    Utils.seperateTimeProgramName(time, out hour, out minute);
                    Program tvProgram = new Program();
                    tvProgram.Name = name.Trim();
                    tvProgram.Date = new DateTime(
                        date.Year,
                        date.Month,
                        date.Day,
                        hour,
                        minute,
                        0
                        );
                    tvProgram.ChannelId = channel.Id;
                    tvProgramList.Add(tvProgram);
                    
                }

                count = count + 2;
            }
            

            return tvProgramList;
        }

        private static List<Program> progNameToProgObjects(string programeName, Channel channel, DateTime date, out string filteredName)
        {
            List<Program> tvProgramList = new List<Program>();

            string programDateExtractor = "\\d\\d?\\.\\d\\d";
            Regex datesForSameProgram = new Regex(programDateExtractor, RegexOptions.IgnoreCase);
            MatchCollection dates = datesForSameProgram.Matches(programeName);
            string programNameExtractor = @"[\s$][^\d\s].*"; //in eter site there can be case when programe name is like 12.00$Armen
            Regex nameProgram = new Regex(programNameExtractor, RegexOptions.IgnoreCase);
            Match match = nameProgram.Match(programeName);
            filteredName = nameProgram.Match(programeName).Value;
            int hour;
            int minute;
            for (int i = 0; i < dates.Count; i++)
            {
                Utils.seperateTimeProgramName(dates[i].Value, out hour, out minute);
                Program tvProgram = new Program();
                tvProgram.Name = filteredName.Trim();
                tvProgram.Date = new DateTime(
                    date.Year,
                    date.Month,
                    date.Day,
                    hour,
                    minute,
                    0
                    );
                tvProgram.ChannelId = channel.Id;
                tvProgramList.Add(tvProgram);

            }
           
            return tvProgramList;
        }
        public static List<Program> ParseAll(DateTime date)
        {
            //TODO Remove debud stuff
            DateTime start = DateTime.Now;
            List<Program> allEterPrograms = new List<Program>();

            foreach (Enums.EterChannels value in Enum.GetValues(typeof(Enums.EterChannels)))
            {
                Channel channel = new Channel();
                ChannelRepository chanRep = ChannelRepository.Instance();
                var id = (int)value;
                channel.Id = id;
                channel.Name = value.ToString();
                //if (chanRep.Get(id).Enabled > 0)
                    allEterPrograms.AddRange(ParseSpecificProgram(channel, date));

            }

            DateTime end = DateTime.Now;
            Console.WriteLine("Parsing for the date " + date.Date + "\nParse time is " + (end.Second - start.Second) + " second");

            return allEterPrograms;
        }

        
    }
}
